package org.kl.bf.service.basic;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.Staffinfo;
import org.kl.bf.entity.basic.User;
//import org.kl.bf.entity.basic.Menu;
import org.kl.bf.repository.basic.StaffinfoDao;
import org.kl.bf.repository.basic.UserDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
//import org.kl.bf.utils.CRUDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.security.utils.Digests;
import org.springside.modules.utils.Encodes;

/**
 * @Author zl
 * @CreateTime 2014-7-16 下午14:31:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class StaffinfoService {
	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	private UserDao userDao;
	private StaffinfoDao staffinfoDao;

	public Staffinfo getStaffinfo(String guid) {
		return staffinfoDao.find(Staffinfo.class, guid);
	}

	public Staffinfo getOneStaffinfo(String guid) {
		return staffinfoDao.findOne(guid);
	}

	public void saveStaffinfo(Staffinfo entity) {
		staffinfoDao.save(entity);
	}

	public void deleteStaffinfo(String guid) {
		staffinfoDao.delete(guid);
	}

	public List<Staffinfo> getAllStaffinfo() {
		return (List<Staffinfo>) staffinfoDao.findAll();
	}

	public Page<Staffinfo> getUserStaffinfo(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<Staffinfo> spec = buildSpecification(searchParams);

		return staffinfoDao.findAll(spec, pageRequest);
	}

	@Deprecated
	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0, j = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				/*User u = userDao.findByStaffinfoId(id);
				if (u == null) {
					staffinfoDao.delete(id);
					i++;
				} else {
					j++;
				}*/
			}
		}
		return "删除" + i + "条记录,忽略" + j + "条关联用户的员工信息";
	}

	/**
	 * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
	 */
	private void entryptPassword(User user) {
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		user.setSalt(Encodes.encodeHex(salt));

		byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(), salt, HASH_INTERATIONS);
		user.setPassword(Encodes.encodeHex(hashPassword));
	}

	/*
	 * 分配登陆账号
	 */
	@Deprecated
	public String fpUser(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0, j = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				/*
				 * User u = userDao.findByStaffinfoId(id); if (u==null) {
				 * //staffinfoDao.delete(id); User user = new User(); Staffinfo
				 * staffinfo = staffinfoDao.findOne(id);
				 * user.setTrueName(staffinfo.getStaffName());
				 * user.setLoginNo(staffinfo.getJobNumber());
				 * user.setPlainPassword(staffinfo.getJobNumber()); //
				 * user.setStaffinfo(staffinfo); entryptPassword(user);
				 * userDao.save(user); i++; } else { j++; }
				 */
			}
		}
		return "成功分配" + i + "个账号,已分配" + j + "各登陆账号";
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Staffinfo> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<Staffinfo> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), Staffinfo.class);
		return spec;
	}

	@Autowired
	public void setStaffinfoDao(StaffinfoDao staffinfoDao) {
		this.staffinfoDao = staffinfoDao;
	}

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public List<Staffinfo> getAllUserStaffinfo(Map<String, Object> searchParams, String sortType, String orderType) {
		Specification<Staffinfo> spec = KLDynamicSpecifications.buildSpecification(searchParams, Staffinfo.class);
		return staffinfoDao.findAll(spec, CRUDUtils.buildSort(sortType, orderType));
	}
}
